<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'sthcloud_sthcloudPpkSloveTask_add'">
            新 增
          </el-button>
          <el-button plain icon="upload-filled" type="primary" class="ml10" @click="excelUploadRef.show()" v-auth="'sys_user_add'">
						导 入
					</el-button>
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
            v-auth="'sthcloud_sthcloudPpkSloveTask_del'" @click="handleDelete(selectObjs)">
            删 除
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'sthcloud_sthcloudPpkSloveTask_export'"
                @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
            @queryTable="getDataList">
						<!-- 新增：任务ID输入框 -->
						<el-input v-model="keyword" size="small" placeholder="请输入任务ID" style="width: 150px;" />

						<!-- 新增：查询按钮 -->
						<el-button icon="search" type="primary" class="ml10" @click="localData">查 询</el-button>

					</right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border 
        :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column prop="id" label="id" show-overflow-tooltip />
          <el-table-column prop="title" label="title"  show-overflow-tooltip/>
          <el-table-column prop="fileType" label="file_type"  show-overflow-tooltip/>
          <el-table-column prop="workdir" label="工作路径"  show-overflow-tooltip/>
          <el-table-column prop="execPpk" label="exec_ppk"  show-overflow-tooltip/>
          <el-table-column prop="sourceClient" label="来源客户端"  show-overflow-tooltip/>
          <el-table-column prop="processStatus" label="process_status"  show-overflow-tooltip/>
          <el-table-column prop="coordinateSystem" label="coordinate_system"  show-overflow-tooltip/>
          <el-table-column prop="ggaStartTime" label="gga开始时间"  show-overflow-tooltip/>
          <el-table-column prop="ggaEndTime" label="gga结束时间"  show-overflow-tooltip/>
          <el-table-column prop="sp3Files" label="sp3_files"  show-overflow-tooltip/>
          <el-table-column prop="diffFiles" label="diff_files"  show-overflow-tooltip/>
          <el-table-column prop="rnxFiles" label="rnx_files"  show-overflow-tooltip/>
          <el-table-column prop="resultFile" label="计算成果文件"  show-overflow-tooltip/>
          <el-table-column prop="message" label="提示信息"  show-overflow-tooltip/>
          <el-table-column prop="cmccDiffFlag" label="cmcc_diff_flag"  show-overflow-tooltip/>
          <el-table-column prop="cmccId" label="中移差分解算任务id"  show-overflow-tooltip/>
          <el-table-column prop="originMileage" label="原始GGA里程数"  show-overflow-tooltip/>
          <el-table-column prop="cmccMileage" label="中移解算里程数"  show-overflow-tooltip/>
          <el-table-column prop="cmccLog" label="cmcc日志"  show-overflow-tooltip/>
          <el-table-column prop="cmccStartTime" label="差分解算任务提交时间"  show-overflow-tooltip/>
          <el-table-column prop="cmccEndTime" label="差分解算任务完成时间"  show-overflow-tooltip/>
          <el-table-column prop="deductionStatus" label="deduction_status"  show-overflow-tooltip/>
          <el-table-column prop="deductionRelateIds" label="deduction_relate_ids"  show-overflow-tooltip/>
          <el-table-column prop="downloadSp3Flag" label="download_sp3_flag"  show-overflow-tooltip/>
          <el-table-column prop="downloadSp3Log" label="sp3文件下载地址"  show-overflow-tooltip/>
          <el-table-column prop="sth2rnxFlag" label="sth2rnx_flag"  show-overflow-tooltip/>
          <el-table-column prop="sth2rnxLog" label="sth转rnx日志"  show-overflow-tooltip/>
          <el-table-column prop="restartFlag" label="重试标识"  show-overflow-tooltip/>
          <el-table-column prop="ppkStartTime" label="PPK开始计算时间"  show-overflow-tooltip/>
          <el-table-column prop="ppkCommand" label="PPK解算任务命令"  show-overflow-tooltip/>
          <el-table-column prop="startTime" label="开始处理时间"  show-overflow-tooltip/>
          <el-table-column prop="endTime" label="结束处理时间"  show-overflow-tooltip/>
          <el-table-column prop="createUser" label="创建人"  show-overflow-tooltip/>
          <el-table-column prop="updateUser" label="更新人"  show-overflow-tooltip/>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button icon="edit-pen" text type="primary" v-auth="'sthcloud_sthcloudPpkSloveTask_edit'"
              @click="formDialogRef.openDialog(scope.row.id)">编辑</el-button>
            <el-button icon="delete" text type="primary" v-auth="'sthcloud_sthcloudPpkSloveTask_del'" @click="handleDelete([scope.row.id])">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />

    <!-- 导入excel (需要在 upms-biz/resources/file 下维护模板) -->
    <upload-excel
			ref="excelUploadRef"
			title="导入"
			url="/sthcloud/sthcloudPpkSloveTask/import"
      temp-url="/admin/sys-file/local/file/sthcloudPpkSloveTask.xlsx"
			@refreshDataList="getDataList"
		/>
  </div>
</template>

<script setup lang="ts" name="systemSthcloudPpkSloveTask">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObjs, getObj } from "/@/api/sthcloud/sthcloudPpkSloveTask";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { useDict } from '/@/hooks/dict';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
const excelUploadRef = ref();
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
	tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/sthcloud/sthcloudPpkSloveTask/export', Object.assign(state.queryForm, { ids: selectObjs }), 'sthcloudPpkSloveTask.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({ id }) => id);
  multiple.value = !objs.length;
};

const keyword = ref('')

const localData = async () => {
	// 将 keyword 转换为整数并放入 queryForm 的 id 字段
	const id = parseInt(keyword.value, 10);
	const taskId = isNaN(id) ? undefined : id;

	try {
		if (taskId !== undefined && taskId > 0) {
			const res = await getObj({ id: taskId });
			state.dataList = res.data || []; // 将返回数据赋值给表格
		} else {
			getDataList(); // 如果没有输入 id，则恢复为分页查询
		}
	} catch (err) {
		useMessage().error('查询失败，请检查输入或网络');
	}finally {
		// 查询完成后清空输入框
		keyword.value = '';
	}
}

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>
